<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue'
import Statistic from './components/Statistic.vue'
import { useUserStore } from '@/stores'
import Home from '@/api/home'
// import * as echarts from 'echarts' // * as 引入所有的echarts
// 按需引入，减少性能开销
// 引入核心包
import * as echarts from 'echarts/core'
// 引入图表类型
import { LineChart, type LineSeriesOption } from 'echarts/charts'
// 引入组件
import { GridComponent, type GridComponentOption } from 'echarts/components'
// 全局过渡动画等特性
import { UniversalTransition } from 'echarts/features'
// 引入Canvas渲染器
import { CanvasRenderer } from 'echarts/renderers'
// 注册
echarts.use([GridComponent, LineChart, CanvasRenderer, UniversalTransition] as any)

type EChartsOption = echarts.ComposeOption<GridComponentOption | LineSeriesOption>

const userStore = useUserStore()
const homeData = ref({} as any)
const messageList = ref([] as any)
const social = ref()
const provident = ref()

const getHomeData = async () => {
  homeData.value = await Home.getHomeData()
}
getHomeData()

const getMessageList = async () => {
  messageList.value = await Home.getMessageList()
}
getMessageList()

let echartsInstance: any = null
let providentInstance: any = null
onMounted(() => {
  // 异步获取数据执行后，很有有可能在渲染后还没有返回结果
  // 监听homeData变化

  // 初始化echarts
  // 需要挂载到dom，所以在onMounted中
  echartsInstance = echarts.init(social.value)
  providentInstance = echarts.init(provident.value)
})

watch(
  () => homeData.value,
  (newValue) => {
    echartsInstance.setOption({
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: homeData.value.socialInsurance?.xAxis
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: homeData.value.socialInsurance?.yAxis,
          type: 'line',
          areaStyle: {
            color: '#04c9be' // 填充颜色
          },
          lineStyle: {
            color: '#04c9be' // 线颜色
          }
        }
      ]
    } as EChartsOption)

    providentInstance.setOption({
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: homeData.value.providentFund?.xAxis
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: homeData.value.providentFund?.yAxis,
          type: 'line',
          areaStyle: {
            color: '#04c9be' // 填充颜色
          },
          lineStyle: {
            color: '#04c9be' // 线颜色
          }
        }
      ]
    } as EChartsOption)
  }
)
</script>

<template>
  <div class="dashboard">
    <div class="container">
      <!-- 左侧内容 -->
      <div class="left">
        <div class="panel">
          <!-- 个人信息 -->
          <div class="user-info">
            <img
              v-if="userStore.user.staffPhoto"
              class="avatar"
              :src="userStore.user.staffPhoto"
              alt=""
            />
            <span v-else class="username">{{ userStore.user.username?.charAt(0) }}</span>
            <div class="company-info">
              <div class="title">
                江苏传智播客教育科技股份有限公司
                <span>体验版</span>
              </div>
              <div class="depart">
                {{ userStore.user.username }} ｜ {{ userStore.user.company }}-{{
                  userStore.user.departmentName
                }}
              </div>
            </div>
          </div>
          <!-- 代办 -->
          <div class="todo-list">
            <Statistic title="组织总人数" :value="homeData.employeeTotal"></Statistic>
            <Statistic title="正式员工" :value="homeData.regularEmployeeTotal"></Statistic>
            <Statistic title="合同待签署" :value="homeData.contractSignTotal"></Statistic>
            <Statistic title="待入职" :value="homeData.toBeEmployed"></Statistic>
            <Statistic title="本月待转正" :value="homeData.toBeConfirmed"></Statistic>
            <Statistic title="本月待离职" :value="homeData.toBeDismissed"></Statistic>
            <Statistic title="接口总访问" :value="homeData.interfaceAccessTotal"></Statistic>
          </div>
        </div>
        <!-- 快捷入口 -->
        <div class="panel">
          <div class="panel-title">快捷入口</div>
          <div class="quick-entry">
            <div class="entry-item">
              <div class="entry-icon approval" />
              <span>假期审批</span>
            </div>
            <div class="entry-item">
              <div class="entry-icon social" />
              <span>社保管理</span>
            </div>
            <div class="entry-item">
              <div class="entry-icon role" />
              <span>角色管理</span>
            </div>
            <div class="entry-item">
              <div class="entry-icon salary" />
              <span>薪资设置</span>
            </div>
            <div class="entry-item">
              <div class="entry-icon bpm" />
              <span>流程设置</span>
            </div>
          </div>
        </div>
        <!-- 图表数据 -->
        <div class="panel">
          <div class="panel-title">社保申报数据</div>
          <div class="chart-container">
            <div class="chart-info">
              <div class="info-main">
                <Statistic
                  title="申报人数"
                  :value="homeData.socialInsurance?.declarationTotal"
                  :class-select="2"
                ></Statistic>
              </div>
              <div class="info-list">
                <Statistic
                  title="待申报(人)"
                  :value="homeData.socialInsurance?.toDeclareTotal"
                  :class-select="2"
                  style="margin-top: 10px"
                ></Statistic>
                <Statistic
                  title="申报中(人)"
                  :value="homeData.socialInsurance?.declaringTotal"
                  :class-select="2"
                ></Statistic>
                <Statistic
                  title="已申报(人)"
                  :value="homeData.socialInsurance?.declaredTotal"
                  :class-select="2"
                ></Statistic>
              </div>
            </div>
            <div class="chart">
              <!-- 图表 -->
              <div ref="social" class="chartContent"></div>
            </div>
          </div>
        </div>
        <!-- 图表数据 -->
        <div class="panel">
          <div class="panel-title">公积金申报数据</div>
          <div class="chart-container">
            <div class="chart-info">
              <div class="info-main">
                <Statistic
                  title="申报人数"
                  :value="homeData.providentFund?.declarationTotal"
                  :class-select="2"
                ></Statistic>
              </div>
              <div class="info-list">
                <Statistic
                  title="待申报(人)"
                  :value="homeData.providentFund?.toDeclareTotal"
                  :class-select="2"
                  style="margin-top: 10px"
                ></Statistic>
                <Statistic
                  title="申报中(人)"
                  :value="homeData.providentFund?.declaringTotal"
                  :class-select="2"
                ></Statistic>
                <Statistic
                  title="已申报(人)"
                  :value="homeData.providentFund?.declaredTotal"
                  :class-select="2"
                ></Statistic>
              </div>
            </div>
            <div class="chart">
              <!-- 图表 -->

              <div ref="provident" class="chartContent"></div>
            </div>
          </div>
        </div>
      </div>
      <!-- 右侧内容 -->
      <div class="right">
        <!-- 帮助链接 -->
        <div class="panel">
          <div class="help">
            <div class="help-left">
              <div class="panel-title">帮助链接</div>
              <div class="help-list">
                <div class="help-block">
                  <i class="icon-entry" />
                  入门指南
                </div>
                <div class="help-block">
                  <i class="icon-help" />
                  在线帮助手册
                </div>
                <div class="help-block">
                  <i class="icon-support" />
                  联系技术支持
                </div>
                <div class="help-block">
                  <i class="icon-add" />
                  添加链接
                </div>
              </div>
            </div>
            <div class="help-right">
              <div class="calendar">
                <!-- <el-calendar /> -->
                <el-calendar />
              </div>
            </div>
          </div>
        </div>
        <!-- 通知公告 -->
        <div class="panel">
          <div class="panel-title">通知公告</div>
          <div class="information-list">
            <div v-for="(item, index) in messageList" :key="index" class="information-list-item">
              <img :src="item.icon" alt="" />
              <div>
                <p>{{ item.notice }}</p>
                <p>{{ item.createTime }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.dashboard {
  background: #f5f6f8;
  width: 100%;
  min-height: calc(100vh - 80px);

  :depp() {
    .el-calendar-day {
      height: 40px;
    }
    .el-calendar-table__row td,
    .el-calendar-table tr td:first-child,
    .el-calendar-table__row td.prev {
      border: none;
    }
  }

  .date-content {
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 14px;
  }
  .date-content .rest {
    color: #fff;
    border-radius: 50%;
    background: rgb(250, 124, 77);
    width: 20px;
    height: 20px;
    line-height: 20px;
    display: inline-block;
    font-size: 12px;
    margin-left: 10px;
  }
  .date-content .text {
    width: 20px;
    height: 20px;
    line-height: 20px;
    display: inline-block;
  }
  :depp() {
    .el-calendar-table td.is-selected .text {
      background: #409eff;
      color: #fff;
      border-radius: 50%;
    }
    .el-calendar__header {
      display: none;
    }
  }
  .container {
    display: flex;
    .right {
      width: 40%;
      .panel {
        margin-left: 8px;
      }
      :nth-child(1) {
        margin-top: 0;
      }
    }
    .left {
      flex: 1;
      :nth-child(1) {
        margin-top: 0;
      }
    }
    .panel {
      background-color: #fff;

      margin-top: 8px;
      padding: 20px;
      .panel-title {
        font-size: 16px;
        color: #383c4e;
        font-weight: 500;
      }
      // 用户信息样式
      .user-info {
        display: flex;
        .avatar {
          width: 48px;
          height: 48px;
          border-radius: 12px;
          background-color: #d9d9d9;
          line-height: 48px;
          text-align: center;
        }
        .username {
          width: 30px;
          height: 30px;
          text-align: center;
          line-height: 30px;
          border-radius: 50%;
          background: #04c9be;
          color: #fff;
          margin-right: 4px;
        }
        .company-info {
          margin-left: 10px;
          height: 48px;
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          .title {
            color: #383c4e;
            font-weight: 500;
            font-size: 16px;
            font-family: PingFang SC, PingFang SC-Medium;
            span {
              font-size: 12px;
              background: #f5f6f8;
              text-align: center;
              padding: 2px 8px;
              border-radius: 2px;
              color: #697086;
            }
          }
          .depart {
            font-size: 14px;
            color: #697086;
            font-weight: 400;
          }
        }
      }
      // 代办样式
      .todo-list {
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;
        .todo-item {
          width: 18%;
          height: 90px;
          display: flex;
          flex-direction: column;
          padding: 10px;
          justify-content: space-around;
          :nth-child(1) {
            color: #697086;
            font-size: 14px;
          }
          :nth-child(2) {
            color: #383c4e;
            font-size: 30px;
            font-weight: 500;
          }
        }
      }
      // 快捷入口
      .quick-entry {
        margin-top: 16px;
        display: flex;
        .entry-item {
          display: flex;
          flex-direction: column;
          align-items: center;
          margin-left: 60px;
          &:nth-child(1) {
            margin-left: 0px;
          }
          .entry-icon {
            width: 40px;
            height: 40px;
            border-radius: 10px;
            background: #f5f6f8;
            background-size: cover;
            &.approval {
              background-image: url('@/assets/common/approval.png');
            }
            &.social {
              background-image: url('@/assets/common/social.png');
            }
            &.salary {
              background-image: url('@/assets/common/salary.png');
            }
            &.role {
              background-image: url('@/assets/common/role.png');
            }
            &.bpm {
              background-image: url('@/assets/common/bpm.png');
            }
          }
          span {
            color: #697086;
            font-size: 14px;
            margin-top: 8px;
          }
        }
      }
      // 图表数据
      .chart-container {
        display: flex;
        .chart-info {
          width: 240px;
          margin-top: 10px;
          .info-main {
            padding: 10px;
            display: flex;
            flex-direction: column;
          }
          .info-list {
            background: #f5f6f8;
            border-radius: 4px;
            padding: 12px 15px;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .info-list-item {
              width: 50%;
              margin-top: 10px;
              display: flex;
              flex-direction: column;

              :nth-child(1) {
                font-size: 14px;
                color: #697086;
              }
              :nth-child(2) {
                margin-top: 10px;
                font-size: 30px;
                color: #383c4e;
                font-weight: 500;
              }
            }
          }
        }
        .chart {
          flex: 1;
          .chartContent {
            // background-color: #04c9be;
            width: 100%;
            height: 100%;
          }
        }
      }
      // 帮助链接
      .help {
        display: flex;
        .help-left {
          width: 40%;
        }
        .help-right {
          flex: 1;
        }
        .help-list {
          .help-block {
            background: #f5f6f8;
            border-radius: 4px;
            width: 264px;
            height: 54px;
            padding: 17px 10px;
            font-size: 14px;
            color: #697086;
            margin-top: 10px;
            i {
              width: 14px;
              height: 14px;
              display: inline-block;
              background-size: cover;
              vertical-align: middle;
            }
            i.icon-help {
              background-image: url('@/assets/common/help.png');
            }
            i.icon-support {
              background-image: url('@/assets/common/support.png');
            }
            i.icon-add {
              background-image: url('@/assets/common/add.png');
            }
            i.icon-entry {
              background-image: url('@/assets/common/entry.png');
            }
          }
        }
      }
      // 通知公告
      .information-list {
        margin-top: 20px;
        .information-list-item {
          display: flex;
          align-items: center;
          margin: 15px 0;
          img {
            width: 40px;
            height: 40px;
            border: 50%;
          }
          .col {
            color: #8a97f8;
          }
          div :nth-child(2) {
            color: #697086;
            font-size: 14px;
          }
        }
      }
    }
  }
}
</style>
